package com.gs.wzmanager.service;

import com.gs.wzmanager.dao.ForcastReviseDao;
import com.gs.wzmanager.entity.ForcastRevise;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/10/31 0031
 */
//文件监听类
@Component
public class MyFileListener extends FileAlterationListenerAdaptor implements ServletContextListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    static ForcastReviseDao dao;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        this.dao = (ForcastReviseDao) WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean("forcastReviseDao");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

    String type;


    public MyFileListener() {

    }

    public MyFileListener(String type) {
        this.type = type;
    }


    @Override
    public void onFileCreate(File file) {

        if (type == "WEEK") {
            BufferedReader br = null;
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
                br = new BufferedReader(isr);
                List<String> sectionList = new LinkedList<>();
                String line;
                String section = "";
                while ((line = br.readLine()) != null) {
                    section += line;
                    if (line.equals("") || line.contains("温州市气象台")) {
                        sectionList.add(section);
                        section = "";
                    }
                }
                trimList(sectionList);
                ForcastRevise forcastRevise = new ForcastRevise();

                logger.info("WEEK sectionList->{}",sectionList);
                logger.info("WEEK time->{}",getTime(sectionList.get(sectionList.size() - 1)));

                String time = getTime(sectionList.get(sectionList.size() - 1));
                forcastRevise.setTime(time);
                String content = "";
                for (int j = 2; j <= sectionList.size() - 2; j++) {
                    content += sectionList.get(j) + "<br/>";
                }

                forcastRevise.setContent(content);

                dao.addReviseWeek(forcastRevise);

                logger.info("WEEKonFileCreate->{}", forcastRevise);
            } catch (FileNotFoundException e) {
                logger.info("dealDirWeekInitFile文件不存在->{}", e.getMessage());
            } catch (IOException e) {
                logger.info("dealDirWeekInitFile文件读取异常->{}", e.getMessage());
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (type == "THREE") {
            BufferedReader br = null;
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
                br = new BufferedReader(isr);
                List<String> sectionList = new LinkedList<>();
                String line;
                String section = "";
                while ((line = br.readLine()) != null) {
                    section += line;
                    if (line.equals("")) {
                        sectionList.add(section);
                        section = "";
                    }
                }
                ForcastRevise forcastRevise = new ForcastRevise();

                logger.info("THREE sectionList->{}",sectionList);
                logger.info("THREE time->{}",getTime(sectionList.get(0)));

                String time = getTime(sectionList.get(0));
                forcastRevise.setTime(time);
                if (sectionList.get(1).startsWith("【温州市区和各县】")) {
                    String content = "";
                    for (int m = 1; m < sectionList.size(); m++) {
                        content += sectionList.get(m) + "<br/>";
                    }
                    forcastRevise.setContent(content);
                } else {
                    forcastRevise.setWarning(sectionList.get(1));
                    String content = "";
                    for (int n = 2; n < sectionList.size(); n++) {
                        content += sectionList.get(n) + "<br/>";
                    }
                    forcastRevise.setContent(content);
                }

                dao.addReviseThree(forcastRevise);

                logger.info("ThreeonFileCreate->{}", forcastRevise);

            } catch (FileNotFoundException e) {
                logger.info("dealDirThreeInitFile文件不存在->{}", e.getMessage());
            } catch (IOException e) {
                logger.info("dealDirThreeInitFile文件读取异常->{}", e.getMessage());
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public void trimList(List<String> list) {
        for (int i = list.size() - 1; i >= 0; i--) {
            if (list.get(i).equals("")) list.remove(i);
        }
    }

    public String getTime(String content) {
        String result = content.substring(content.indexOf("温州市气象台") + 6, content.indexOf("发布"));
        return result;
    }

}


